package com.leyou.item.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.pojo.PageResult;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.mapper.GoodsMapper;
import com.leyou.item.pojo.Brand;
import com.leyou.item.pojo.Spu;
import com.leyou.item.service.IBrandService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author CHERRY
 * @date 2018-09-02-11:00
 */
@Service
public class BrandServiceImpl implements IBrandService {
    @Autowired
    private BrandMapper brandMapper;
    /**
     * 商品Mapper接口
     */
    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public PageResult queryBrandByPage(String key, Integer page, Integer rows, String sortBy, Boolean desc) {

        Example example = new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();
        //搜索词不为空
        if (StringUtils.isNotBlank(key)) {
            criteria.andLike("name", "%" + key + "%").andEqualTo("letter", key);
        }
        //排序 字段
        if (StringUtils.isNotBlank(sortBy)) {
            example.setOrderByClause(sortBy + (desc ? " DESC" : " ASC"));
        }
        //分页插件
        PageHelper.startPage(page, rows);
        List<Brand> brands = brandMapper.selectByExample(example);
        PageInfo<Brand> pageInfo = new PageInfo<>(brands);

        return new PageResult(pageInfo.getTotal(), pageInfo.getList());
    }

    @Transactional
    @Override
    public void insertBrand(List<Long> cids, Brand brand) {
        //新增品牌信息
        this.brandMapper.insertSelective(brand);
        //新增分类和品牌中间表
        cids.forEach(cid -> {
            //调用回写的主键进行插入
            this.brandMapper.insertCategoryBrand(cid, brand.getId());
        });

    }

    @Transactional
    @Override
    public void updateBrand(Brand brand, List<Long> cids) {
        //修改品牌信息
        this.brandMapper.updateBrand(brand);
        //删除当前所属分类
        this.brandMapper.deleteCategoryBid(brand.getId());
        //新增分类品牌中间表
        cids.forEach(cid -> this.brandMapper.insertCategoryBrand(cid, brand.getId()));

    }

    /**
     * 根据bid删除品牌信息和所属分类数据
     *
     * @param bid
     */
    @Transactional
    @Override
    public void deleteBrandByBid(Long bid) {
        //删除分类和品牌中间表数据
        this.brandMapper.deleteCategoryBid(bid);
        //删除品牌数据
        this.brandMapper.deleteByPrimaryKey(bid);
        //更改spu表 品牌valid
        Spu spu = new Spu();
        spu.setBrandId(bid);
        //根据品牌查询Spu集合
        List<Spu> spus = this.goodsMapper.select(spu);
        //遍历查询主键
        spus.forEach(newSpu -> {
            Spu updateSpu = new Spu();
            //更新的主键
            updateSpu.setId(newSpu.getId());
            //更新valid 为false
            updateSpu.setValid(false);
            //通用mapper主键修改valid
            this.goodsMapper.updateByPrimaryKeySelective(updateSpu);
        });
    }

    @Override
    public List<Brand> queryBrandByCid(Long cid) {
        List<Brand> brands = this.brandMapper.queryBrandByCid(cid);
        return brands;
    }

    @Override
    public Brand queryBrandByBid(Long id) {

        return this.brandMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Brand> queryBrandByIds(List<Long> ids) {
        List<Brand> brands = this.brandMapper.selectByIdList(ids);
        return brands;
    }
}
